package com.hottv.meeting.videomeeting.module.edp;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.caption.netmonitorlibrary.netStateLib.NetChangeObserver;
import com.caption.netmonitorlibrary.netStateLib.NetStateReceiver;
import com.caption.netmonitorlibrary.netStateLib.NetUtils;
import com.chinamobile.iot.onenet.edp.toolbox.EdpClient;
import com.hottv.meeting.videomeeting.module.meeting.InfoAccessImpl;

/**
 * Created by chengwenqiang on 2019/6/4.
 */

public class EDPService extends Service {

    private final int MAX_DURATION_TIME = 1000 * 60 * 10; // 最大没接收到数据的时间，超过这个时间就重新注册
    private Context mConetxt;
    protected NetChangeObserver mNetChangeObserver = null;  //网络观察者
    private EDPListener edpListener;

    public static  long mCurrent = System.currentTimeMillis();
    /**
     * 分钟广播
     */
    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {
                Log.d("HeartBeatService", "heartbeat");
                long time = System.currentTimeMillis() - mCurrent;
                if (time > MAX_DURATION_TIME) {
                    EDPUtils.bindDevices(mConetxt, edpListener);
                }
            }
        }
    };
    public EDPService() {
        super();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
        mConetxt = this;

//        InfoAccessImpl.getInstance(mConetxt);

        mNetChangeObserver = new NetChangeObserver() {
            @Override
            public void onNetConnected(NetUtils.NetType type) {
                Log.v(EDPConfig.TAG, "onNetConnected");
                EDPUtils.bindDevices(mConetxt, edpListener);
            }

            @Override
            public void onNetDisConnect() {
                Log.v(EDPConfig.TAG, "onNetDisConnect");
            }
        };
        //开启广播去监听 网络 改变事件
        NetStateReceiver.registerObserver(mNetChangeObserver);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mConetxt = this;
        return START_STICKY;
    }

    private void init() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_TIME_TICK);
        registerReceiver(receiver, filter);
        edpListener = new EDPListener(this);
        EDPUtils.bindDevices(this, edpListener);
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onRebind(Intent intent) {
        super.onRebind(intent);
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }
}
